/*****************************************************************
**                                                              **
**  Copyright (C) 2007 Amlogic,Inc.                             **
**  All rights reserved                                         **
**  CY8C21434 Touch Panel Input                                 **
*****************************************************************/
#include "includes.h"

#include "ioapi.h"

#include "chipsupport.h"



#if (defined AML_ARES)||(defined AML_COPIA)||(defined AML_NIKE)	
#ifdef ARES_SOFT_I2C
#include "Drivers/i2c/soft_i2c.h"
#else
#include "Drivers/i2c/hard_i2c.h"
#endif 
#else 
#include "Drivers/i2c/soft_i2c.h"
#endif

int I2C_device_fd = -1;
int i2c_handle_flag = 0;

void Init_I2C_parameters(void)
{
#if (defined AML_ARES)||(defined AML_COPIA)||(defined AML_NIKE)	
#ifdef ARES_SOFT_I2C
    soft_i2c_para_t temp_i2c_con = {
    	  1,    //unsigned            magic;
        "/dev/soft_i2c",   //char       *device_name
	0xa0,   //unsigned char       device_id;
        0xff,   //unsigned char       id_mask;
        0,      //unsigned char       i2c_flag    
        512,    //unsigned            max_addr;
        200,    //unsigned            delay; requency is about 100khz
        0x01200030,   //unsigned            scl_en_reg;         //m2_a8  is muxed to scl
        0x01000000,   //unsigned            scl_en_mask; 
        
        0x01200038,   //unsigned            scl_output_reg; 
        0x01000000,   //unsigned            scl_output_mask; 
      
        0x01200030,   //unsigned            sda_en_reg;                  //m2_a9  is muxed to sda
        0x02000000,   //unsigned            sda_en_mask; 
       
        0x01200038,   //unsigned            sda_output_reg;      
        0x02000000,   //unsigned            sda_output_mask; 
    
        0x01200040,   //unsigned            sda_input_reg;         
        0x02000000,   //unsigned            sda_input_mask;   
      };
#else
    hard_i2c_para_t temp_i2c_con = {
    	  0,    //unsigned            magic;
        "/dev/hard_i2c",    //char       *device_name;
        0xa0,               //unsigned char       device_id;
        0xff,               //unsigned char       id_mask;
        0,                  //unsigned char       i2c_flag    
        512,                //unsigned            max_addr;
        1,                  //unsigned            delay;
        400   //100,       //unsigned            requency
    };
#endif

#else
    soft_i2c_para_t temp_i2c_con = {
    	  1,    //unsigned            magic;
        "/dev/soft_i2c",   //char       *device_name
	      0xa0,   //unsigned char       device_id;
        0xff,   //unsigned char       id_mask;
        0,      //unsigned char       i2c_flag    
        512,    //unsigned            max_addr;
        200,    //unsigned            delay; requency is about 100khz
        0x012000A0,   //unsigned            scl_en_reg;  //FEC_D_0 is muxed to scl
        0x20000000,   //unsigned            scl_en_mask; 
        
        0x012000A4,   //unsigned            scl_output_reg; 
        0x20000000,   //unsigned            scl_output_mask; 
      
        0x012000A0,   //unsigned            sda_en_reg;                  //FEC_D_1 is muxed to sda
        0x10000000,   //unsigned            sda_en_mask; 
       
        0x012000A4,   //unsigned            sda_output_reg;      
        0x10000000,   //unsigned            sda_output_mask; 
    
        0x012000A8,   //unsigned            sda_input_reg;         
        0x10000000,   //unsigned            sda_input_mask;   
      };

#endif
          

    if(I2C_device_fd < 0)    
        I2C_device_fd = open(temp_i2c_con.device_name, O_RDWR); 

    ioctl(I2C_device_fd, INIT_I2C_PIN_MUX, &temp_i2c_con); 
}


void modify_i2c_id(unsigned char id)
{
	ioctl(I2C_device_fd, MODIFY_DEVICE_ID, id); 		
	return;
}


void modify_i2c_flag(unsigned char flag)
{
	ioctl(I2C_device_fd, MODIFY_I2C_FLAG, flag); 
	return;		
}


void modify_i2c_delay(unsigned delay)
{
	ioctl(I2C_device_fd, MODIFY_I2C_DELAY, delay); 
	return;		
}

void i2c_delay_ms(unsigned int count)
{
	if(count)
		{
		  for(int j = 0; j < count; j ++)
		  {
			  for(int k = 0; k < 36000; k++);
		  }
		}
}

//int i2c_read_rtc(unsigned char *buff, unsigned bytes, unsigned addr )
//{
//   int i2c_rw_flag = -1 ;
//   if((I2C_device_fd > 0) && (i2c_handle_flag == 0))
//   	{
//   	     i2c_handle_flag = 1; 
//	      modify_i2c_id(0xde);
//	      i2c_rw_flag = read(I2C_device_fd, buff , bytes | (addr << 16));
//	      i2c_handle_flag = 0;
//	 }
//	 	
//   return i2c_rw_flag;
//}
//
//int i2c_write_rtc(unsigned char *buff, unsigned bytes, unsigned addr )
//{
//   int i2c_rw_flag = -1 ;
//   if((I2C_device_fd > 0) && (i2c_handle_flag == 0))
//   	{
//   	      i2c_handle_flag = 1; 
//	      modify_i2c_id(0xde);
//	      i2c_rw_flag = write(I2C_device_fd, buff , bytes | (addr << 16));
//	      i2c_handle_flag = 0;
//	 }
//	
//   return i2c_rw_flag;
//}

int i2c_read_eeprom(unsigned char *buff, unsigned bytes, unsigned addr )
{
   int i2c_rw_flag = -1 ;
   if((I2C_device_fd > 0) && (i2c_handle_flag == 0))
   	{
   		  i2c_handle_flag = 1; 
	      modify_i2c_id(0xa0);     
	      i2c_rw_flag = read(I2C_device_fd, buff , bytes | (addr << 16));
	      i2c_handle_flag = 0;
	 }		
   return i2c_rw_flag;
}

int i2c_write_eeprom(unsigned char *buff, unsigned bytes, unsigned addr )
{
   int i2c_rw_flag = -1 ;
   int index, temp ;
   if((I2C_device_fd > 0) && (i2c_handle_flag == 0))
   	{
   		  i2c_handle_flag = 1; 
	      modify_i2c_id(0xa0);
//#if (defined AML_ARES)||(defined AML_COPIA)	||(defined AML_NIKE)	      
	      for(index = 0; index < bytes ; )
	      {
	      	  if((bytes - index) > 8 )
	      	  	temp = 8;
	      	  else
	      	  	temp = bytes - index; 	
	          i2c_rw_flag = write(I2C_device_fd, buff , temp | ((addr + index) << 16));
	          index += temp;
	          buff += temp;
//	          if(bytes > index)
	             i2c_delay_ms(5);
	      }
//#else	      
//	       for(index = 0; index < bytes ; index++)
//	      {
//	         i2c_rw_flag = write(I2C_device_fd, buff , 1 | ((addr + index) << 16));
//	         buff++;
//	       }
//#endif	       
	      i2c_handle_flag = 0;
	 }	
   return i2c_rw_flag;
}
